function Position(lft, tp) {
	this.left = lft;
	this.top = tp;
}

// helper methods
function addCss(doc, cssCode) {
	var styleElement = doc.createElement("style");
	styleElement.type = "text/css";
	if (styleElement.styleSheet) {
		styleElement.styleSheet.cssText = cssCode;
	} else {
		styleElement.appendChild(doc.createTextNode(cssCode));
	}
	content.document.getElementsByTagName("head")[0].appendChild(styleElement);
}

// finds the position of current element
function getPosition(elem) {
	var pos = new Position(elem.offsetLeft, elem.offsetTop);

	var tmpElem = elem.offsetParent;
	while (tmpElem != null) {
		pos.left += tmpElem.offsetLeft;
		pos.top += tmpElem.offsetTop;
		tmpElem = tmpElem.offsetParent;
	}
	return pos;
}

function get_XPath(elt) {
	if (!$) {
		$ = CultureMuncher.jQuery;
	}
	var path = '';
	for (; elt && elt.nodeType==1; elt=elt.parentNode) {
		var idx=$(elt.parentNode).children(elt.tagName).index(elt)+1;
		//idx>1 ? (idx='['+idx+']') : (idx='');
		idx='['+idx+']';
		path='/'+elt.tagName.toLowerCase()+idx+path;
	}
	return path;
}

function selectNodeXPath(doc, xpath) {
	return doc.evaluate(xpath, doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}

function selectNodesXPath(doc, xpath) {
	return doc.evaluate(xpath, doc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
}

String.prototype.makeXul = function() {
	return this.replace(/(<[a-zA-Z]+)/g, '$1 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"');
}
String.prototype.regexIndexOf = function(regex, startpos) {
	var indexOf = this.substring(startpos || 0).search(regex);
	return (indexOf >= 0) ? (indexOf + (startpos || 0)) : indexOf;
}
String.prototype.regexLastIndexOf = function(regex, startpos) {
	regex = (regex.global) ? regex : new RegExp(regex.source, "g" + (regex.ignoreCase ? "i" : "") + (regex.multiLine ? "m" : ""));
	if(typeof (startpos) == "undefined") {
		startpos = this.length;
	} else if(startpos < 0) {
		startpos = 0;
	}
	var stringToWorkWith = this.substring(0, startpos + 1);
	var lastIndexOf = -1;
	var nextStop = 0;
	while((result = regex.exec(stringToWorkWith)) != null) {
		lastIndexOf = result.index;
		regex.lastIndex = ++nextStop;
	}
	return lastIndexOf;
}
String.prototype.replaceAll= function(s1, s2) {
	return this.split(s1).join(s2);
}
String.prototype.regExReplaceAll= function(s1, s2) {
	var re = new RegExp(s1);
	var ret = this;
	var m = re.exec(ret);
	if (m == null) {
		return this;
	} else {
		for (i = 1; i < m.length; i++) {
			ret = ret.replace(m[i], m[i] + s2);
		}
	}

	return ret;
}
String.prototype.padLeft = function(n, pad) {
	var t = '';
	if(n>this.length) {
		for(var i=0;i < n-this.length;i++) {
			t+=pad;
		}
	}
	return t+this;
}

ArrayRemove = function(arr, s) {
  var i = arr.indexOf(s);
  if(i != -1) arr.splice(i, 1);
}